<template>
  <div>
    <!-- 一条文章单元格 -->
    <van-cell @click="detailFn(listInfo.art_id)">
      <!-- 标题区域的插槽 -->
      <template #title>
        <div class="title-box">
          <!-- 标题 -->
          <span>{{ listInfo.title }}</span>
          <img
            class="thumb"
            v-if="listInfo.cover.type === 1"
            :src="listInfo.cover.images[0]"
          />
        </div>
        <div class="thumb-box" v-if="listInfo.cover.type > 1">
          <img
            class="thumb"
            v-for="(imgUrl, index) in listInfo.cover.images"
            :key="index"
            :src="imgUrl"
          />
        </div>
      </template>
      <!-- label 区域的插槽 -->
      <template #label>
        <div class="label-box">
          <div>
            <span>{{ listInfo.aut_name }}</span>
            <span>{{ listInfo.comm_count }}评论</span>
            <span>{{ timeFormat(listInfo.pubdate) }}</span>
          </div>
          <!-- 反馈按钮 -->
          <van-icon name="cross" @click="show = true" />
        </div>
      </template>
    </van-cell>

    <van-action-sheet
      v-model="show"
      :actions="actions"
      @select="onSelect"
      @cancel="cancelFn"
      @close="closeFn"
      get-container="body"
      :cancel-text="botterText"
    />
  </div>
</template>

<script>
import { timeAgo } from "@/utils/day.js";
import { firstActions, secondActions } from "@/api/report.js";
import { dislikeArticle, reportsArticle } from "@/api";
import { Notify } from "vant";
export default {
  name: "ArticleItem",
  data() {
    return {
      show: false,
      actions: firstActions,
      botterText: "取消",
    };
  },
  props: ["listInfo"],
  methods: {
    timeFormat: timeAgo,
    onSelect(action) {
      if (action.name === "反馈垃圾内容") {
        this.actions = secondActions;
        this.botterText = "返回";
      } else if (action.name === "不感兴趣") {
        this.dislikeFu();
        this.show = false;
      } else {
        this.reportsFn(action.name);
        this.show = false;
      }
    },
    cancelFn() {
      if (this.botterText === "返回") {
        this.show = true;
        this.actions = firstActions;
        this.botterText = "取消";
      }
    },
    closeFn() {
      this.actions = firstActions;
      this.botterText = "取消";
    },
    async dislikeFu() {
      const res = await dislikeArticle(this.listInfo.art_id);
      try {
        if (res.status == 201) {
          Notify({ type: "success", message: "反馈成功！" });
        }
      } catch (error) {
        Notify({ type: "warning", message: "登录过期，请重新登录" });
      }
    },
    async reportsFn(type) {
      let res = await reportsArticle({
        artId: this.listInfo.art_id,
        type,
      });
      try {
        if (res.status == 201) {
          Notify({ type: "success", message: "举报成功！" });
        }
      } catch (error) {
        Notify({ type: "warning", message: "举报失败，请重新登录" });
      }
    },
    detailFn(detail_id) {
      if (this.show === false)
        this.$router.push({
          path: `/detail?art_id=${detail_id}`,
        });
    },
  },
};
</script>

<style scoped lang="less">
/* 标题样式 */
.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

/* label描述样式 */
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 文章信息span */
.label-box span {
  margin: 0 3px;
  &:first-child {
    margin-left: 0;
  }
}

/* 图片的样式, 矩形黄金比例：0.618 */
.thumb {
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

/* 三图, 图片容器 */
.thumb-box {
  display: flex;
  justify-content: space-between;
}
</style>
